<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="经纪人名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入经纪人名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属门店名称" prop="storeName">
        <el-input
          v-model="queryParams.storeName"
          placeholder="请输入所属门店名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="经纪人手机号" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入经纪人手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['broker:broker:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['broker:broker:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['broker:broker:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="brokerList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column label="经济人id" prop="id" />
      <el-table-column label="经纪人名称" prop="name" />
      <el-table-column label="经纪人头像" prop="avatar" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.avatar" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="所属门店名称" prop="storeName" />
      <el-table-column label="经纪人手机号" prop="mobile" />
      <el-table-column label="经纪人资格证书" prop="mobile">
        <template slot-scope="scope">
          <el-carousel indicator-position="outside">
            <el-carousel-item
              v-for="item in scope.row.imageList.length"
              :key="item"
            >
              <image-preview
                :src="scope.row.imageList[item - 1].image"
                :width="200"
                :height="300"
              />
            </el-carousel-item>
          </el-carousel>
        </template>
      </el-table-column>
      <el-table-column label="操作" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['broker:broker:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['broker:broker:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改经纪人对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="经纪人名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入经纪人名称" />
        </el-form-item>
        <el-form-item label="经纪人头像" prop="avatar">
          <image-upload v-model="form.avatar" />
        </el-form-item>
        <el-form-item label="所属门店" prop="avatar">
          <el-select v-model="form.storeId" placeholder="请选择">
            <el-option
              v-for="item in Sortlist"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="经纪人手机号" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入经纪人手机号" />
        </el-form-item>
        <el-form-item label="经纪人的资格证书" prop="mobile">
          <el-upload
            v-model="form.imageList"
            action="http://127.0.0.1:8080/broker/broker/BrokderOss"
            list-type="picture-card"
            :on-preview="handlePictureCardPreview"
            :on-remove="handleRemove"
            :on-success="handleAvatarSuccess"
            :file-list="seizlist"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="" />
          </el-dialog>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listBroker,
  getBroker,
  delBroker,
  addBroker,
  updateBroker,
  storelist,
  InquireBroker,
} from "@/api/broker/broker";

export default {
  name: "Broker",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 经纪人表格数据
      brokerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        storeName: null,
        mobile: null,
      },
      // 表单参数
      form: {
        imageList: [],
      },
      // 表单校验
      rules: {},
      Sortlist: [],
      seizlist: [],
    };
  },
  created() {
    this.getList();
    this.Mysorelist();
  },
  methods: {
    /** 查询经纪人列表 */
    getList() {
      this.loading = true;
      listBroker(this.queryParams).then((response) => {
        this.brokerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        avatar: null,
        storeName: null,
        mobile: null,
        storeId: null,
        imageList: [],
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.form.imageList = [];
      this.seizlist = [];
      this.title = "添加经纪人";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.form = row;
      for (let index = 0; index < row.imageList.length; index++) {
        this.seizlist.push({
          url: row.imageList[index].image,
        });
      }
      console.log(this.seizlist);
      this.open = true;
      this.title = "修改经纪人";
    },
    Mysorelist() {
      storelist()
        .then((res) => {
          console.log("hahahahah" + res.data);
          this.Sortlist = res;
        })
        .catch((err) => {});
    },

    //oss图片上传
    handleRemove(file, fileList) {
      debugger;
      // 1.获取将要删除图片的临时路径
      console.log("nihao" + file);
      //添加时候 的临时路径
      if (this.form.id == null) {
        const filePath = file.response.data;
        // 2.从pics数组中，找到图片对应的索引值
        const i = this.form.imageList.indexOf(filePath);
        // 3.调用splice方法，移除图片信息
        this.form.imageList.splice(i, 1);
      }
      //修改回显的路径
      else {
        const filePath1 = file.image;
        // 2.从pics数组中，找到图片对应的索引值
        const i1 = this.form.imageList.indexOf(filePath1);
        // 3.调用splice方法，移除图片信息
        this.form.imageList.splice(i1, 1);
        this.seizlist.splice(i1, 1);
      }
      console.log(file, fileList);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    handleAvatarSuccess(res, file) {
      this.form.imageList.push({ image: res.data });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateBroker(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBroker(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除经纪人编号为"' + ids + '"的数据项？')
        .then(function () {
          return delBroker(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "broker/broker/export",
        {
          ...this.queryParams,
        },
        `broker_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
